
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getwidget/getwidget.dart';


class SearchBoxWidget extends StatefulWidget {
  //点击搜索时的回调
  final Function(String)? okSearch;
  final Function(String)? textChange;
  final TextEditingController _textEditingController = TextEditingController();
  final String? text;
  final String? hintText;
  SearchBoxWidget({
    Key? key,
    this.okSearch,
    this.textChange,
    this.text,
    this.hintText = "关键词搜索",
  }) : super(key: key);

  String get searchText {
    return _textEditingController.text;
  }

  @override
  State<SearchBoxWidget> createState() => _SearchBoxWidgetState();
}

class _SearchBoxWidgetState extends State<SearchBoxWidget> {
  bool _cancelVisable = true;
  @override
  void initState() {
    super.initState();
    widget._textEditingController.text = widget.text ?? "";
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: double.infinity,
      height: 32.0,
      child: Row(
        children: [
          Expanded(
            flex: 1,
            child: Container(
              height: double.infinity,
              margin: const EdgeInsets.only(left: 16),
              decoration: BoxDecoration(
                  color: const Color.fromRGBO(235, 238, 241, 0.78),
                  borderRadius: BorderRadius.circular(4)),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children:   [
                  const Padding(
                    padding: EdgeInsets.only(left: 8),
                  ),
                  const Icon(Icons.search_rounded,color: Colors.grey,),
                  Expanded(
                    flex: 1,
                    child:Container(
                      alignment: Alignment.center,
                      child:  TextField(
                        textInputAction: TextInputAction.search,
                        cursorColor: Colors.black38,
                        cursorWidth: 1.0,
                        cursorHeight:22.0 ,
                        controller: widget._textEditingController,
                        autofocus: false,
                        style: const TextStyle(fontSize: 14, color: Colors.black87),
                        decoration:  InputDecoration(
                          contentPadding: const EdgeInsets.all(0.0),
                          hintText: widget.hintText ?? "",
                          border: const OutlineInputBorder(borderSide: BorderSide.none),
                          // focusedBorder: InputBorder.none,
                          // enabledBorder: InputBorder.none,
                        ),
                        maxLines: 1,
                        onChanged: (String text){
                          print("onChanged searchText===$text");
                          bool newTextStatus =( (text ?? "").trim().isBlank ?? true);
                          setState(() {
                              _cancelVisable = newTextStatus;
                          });
                          if(widget.textChange != null){
                              widget.textChange!(text);
                          }
                        },
                        onSubmitted: (String text){
                          print("搜索==== $text");
                          if(widget.okSearch != null){
                            widget.okSearch!(text);
                          }
                        },
                      ),
                    ),
                  ),
                  const Padding(padding: EdgeInsets.only(right: 8)),
                  Offstage(
                    offstage: _cancelVisable,
                    child:GestureDetector(
                      onTap: (){
                        widget._textEditingController.clear();
                         setState(() {
                           _cancelVisable = true;
                         });
                      },
                      child:const Icon(Icons.cancel, color: Colors.black38) ,
                    )  ,
                  ),
                  const Padding(padding: EdgeInsets.only(right: 8))
                ],
              ),
            ),
          ),
          const Padding(padding: EdgeInsets.only(right: 8,left: 8)),
          GFButton(
            color: GFColors.DARK,
            padding: const EdgeInsets.symmetric(horizontal: 3),
            size: GFSize.SMALL,
            onPressed: (){
              var text = widget.searchText;
              print("searchText===>$text");
              widget.okSearch!(text);
            },
            text: "搜索",
          ),
          const Padding(padding: EdgeInsets.only(right: 8,left: 8))
          // GestureDetector(
          //   onTap: () {
          //     var text = sbc.searchTextEditController.text;
          //     print("searchText===>$text");
          //   },
          //   child: Container(
          //     color: Colors.black,
          //     padding: const EdgeInsets.only(left: 16, right: 16),
          //     child: const Text("搜索",
          //         style: TextStyle(fontSize: 16, color: Color(0xFF3D7DFF))),
          //   ),
          // ),
          // const Padding(padding: EdgeInsets.only(right: 8,left: 8))
        ],
      ),
    );
  }
}
